<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;

use  App\Models\Tips;
use App\Models\Achievement;


class TipsController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $Tips=Tips::where("corpid", $this->corpid)->get();
  
            $view = 'admin.achievement.add-tips';

        return view($view)->with('user', $this->user)->with('Tips', $Tips);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        dd(11);
        //
    }

    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $tipsNum = Tips::where("corpid", $this->corpid)->count();
        if ($tipsNum>50) {
            return $this->formatJson('error', '标签数量超过限制');
        }
        $name =trim($request->name);
        $id = $request->input('id',0);
        if($id > 0){
            $tips = Tips::where("corpid", $this->corpid)->find($id);
            if(empty($tips)){
                return $this->formatJson('error', '不存在');
            }
            $col=["#d05ef9","#f39b77","#acd697","#facd89","#8c98cc","#84ccc9","#89c997","#c9b4dd","#89abd9"];
            
            $tips->name = $name;
            $tips->save();

            return $this->formatJson('success', '标签修改成功',$tips);
        }else{
            $tNum = Tips::where("corpid", $this->corpid)->where("name",$name)->count();
            if (  $tNum>=1) {
                return $this->formatJson('error', '已经有相同的标签了');
            }

            $col=["#d05ef9","#f39b77","#acd697","#facd89","#8c98cc","#84ccc9","#89c997","#c9b4dd","#89abd9"];
            $tips = new Tips;
            $tips->name = $name;
            $tips->user_id = $this->user->id;
            $tips->corpid = $this->corpid;
            $tips->color=$col[$tipsNum%8];
            $tips->save();

            return $this->formatJson('success', '标签添加成功',$tips);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(request $request, $id)
    {

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $t=Tips::find($id);
        $a=Achievement::where('tip_id',$id)->get();
        foreach ($a as $k =>$value){
            $value->tip_id=Null;
            $value->save();
        }
        if ($t->delete()){
            return $this->formatJson('success', '删除成功');
        }else{
            return $this->formatJson('error', '删除失败');
        }
    }


    public function tigShow(Request $request){
        dd(111);
    }
}
